﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;
using Runda.EShop.Common.Component;
using Runda.EShop.Common.Utility;

namespace Runda.EShop.Web.Customer
{
    public partial class OrderAddr :BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtHopeDate.Attributes.Add("onclick", "WdatePicker();");
            txtHopeDate.Text = System.DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        private void Initialize()
        {
            GetAddress();
        }

        private void GetAddress()
        {
            List<BizEntAddrEntity> list = new CustomerBll().GetAll<BizEntAddrEntity>(" and EntID = '" + base.User.Identity.LoginUser.EntID + "'");
            gvList.DataSource = list;
            gvList.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Guid AddressID = Guid.Empty;
            foreach (GridViewRow row in gvList.Rows)
            {
                RadioButton rbSelect = row.FindControl("rbSelect") as RadioButton;
                if (rbSelect.Checked)
                {
                    AddressID = new Guid(gvList.DataKeys[row.RowIndex]["ID"].ToString());
                    break;
                }
            }

            

            if (AddressID == Guid.Empty) return;
            OrderBll bll = new OrderBll();
            BizEntAddrEntity addrEnity = bll.Get<BizEntAddrEntity>(AddressID);
            if (addrEnity == null) return;

            BizOrderEntity orderEntity = new BizOrderEntity();
            orderEntity.CustomerID = base.User.Identity.LoginUser.UserID;
            orderEntity.OrderDate = DateTime.Now;
            orderEntity.OrderStatus = (int)EnumHelper.OrderStatus.Submit;
            orderEntity.OrderType = Convert.ToInt32(rblOrderType.SelectedValue);
            orderEntity.CreatedOn = DateTime.Now;
            orderEntity.ReceiveName = addrEnity.Name;
            orderEntity.ReceiverPhone = addrEnity.Phone;
            orderEntity.ReceiveAddr = addrEnity.Address;
            orderEntity.Memo = txtMemo.Text;
            orderEntity.Grade = 0;
            orderEntity.Deleted = false;

            DateTime dt = DateTime.Now;

            if (DateTime.TryParse(txtHopeDate.Text, out dt))
            {
                orderEntity.HopeDate = dt;
            }

            bll.CreateOrder(orderEntity,base.User.Identity.LoginUser);

            string str = base.EncryptQueryString("Code=" + orderEntity.OrderCode);

            Response.RedirectPermanent("OrderConfirm.aspx?" + str);
        }

        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid ID = new Guid(gvList.DataKeys[e.RowIndex]["ID"].ToString());

            CustomerBll bll = new CustomerBll();
            bll.Delete<BizEntAddrEntity>(" and ID = '" + ID + "'");

            GetAddress();
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rbSelect = e.Row.FindControl("rbSelect") as RadioButton;
                BizEntAddrEntity entity = e.Row.DataItem as BizEntAddrEntity;
                rbSelect.Checked = entity.IsDefault == "Y" ? true : false; 
            }
        }

        protected void lnkbtnAdd_Click(object sender, EventArgs e)
        {
            BizEntAddrEntity entity = new BizEntAddrEntity();
            entity.EntID = base.User.Identity.LoginUser.EntID;
            entity.IsDefault = "N";
            entity.Name = txtName.Text;
            entity.Phone = txtPhone.Text;
            entity.PostCode = txtPostCode.Text;
            entity.Address = txtAddress.Text;

            CustomerBll bll = new CustomerBll();
            bll.Insert(entity);

            txtAddress.Text = string.Empty;
            GetAddress();
        }
    }
}